const pool = require('../tools/getPool');
const sendResponse = require('../tools/sendResponse');

module.exports = function(req, res){

	let author_id = req.body.author_id;
	let module_id = req.body.module_id;
	let coverimg  = req.body.coverimg;
	let title  	  = req.body.title;
	let content   = req.body.content; 

	if (author_id == undefined || module_id == undefined || coverimg == undefined || title == undefined || content == undefined) {
		sendResponse('error','缺少内容，请检查', null, res);
		return;
	}

	// 获取数据库连接
	pool.getConnection(function(err, connection) {
		if (err) {
			console.log(err);
			sendResponse('error','数据库内部错误', null, res);
			return;
		}

		let params = [author_id,module_id,coverimg,title,content];
		// 执行语句
		connection.query('INSERT INTO news (author_id, module_id, coverimg, title, content) VALUES (?,?,?,?,?);', params, function (error, rows, fields) {
			// 释放连接
			connection.release();

			if (error) { 
				console.log(error);
				sendResponse('error','数据库内部错误', null, res);
				return;
			}

			sendResponse('ok','发表成功', rows, res);

		})	

	});

}